import sqlite3


# sqlite 数据库操作

# 查询数据
def select(table, field, where):
    con = connect_database()
    value = con.execute('SELECT %s FROM %s where %s' % (field, table, where))
    for row in value:
        return row
    con.close()


# 插入数据
def insert(table, field, value):
    con = connect_database()
    con.execute("INSERT INTO %s (%s) VALUES (%s)" % (table, field, value))
    # con.execute("INSERT INTO image (image_name,image_url) VALUES ('name','baidu')")
    con.commit()
    con.close()


# 更新数据
def update(table, vale, where):
    con = connect_database()
    con.execute("UPDATE %s SET %s WHERE %s" % (table, vale, where))
    con.commit()
    con.close()


# 创建数据库
def create_table(sql):
    con = connect_database()
    con.execute(sql)
    # con.execute('''CREATE TABLE if NOT EXISTS image (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,image_name TEXT
    # NOT NULL ,image_url TEXT NOT NULL , status INT NOT NULL DEFAULT (1),type INT NOT NULL DEFAULT (1));''')


# 连接数据库
def connect_database():
    con = sqlite3.connect('database.db')
    return con


# 创建数据库
def create_database():
    connect_database()
    print('创建数据库完毕')
